package com.zzw.hj39;

import java.util.Scanner;

/**
 * @Project: hw_java
 * @Description: 判断两个IP是否属于同一子网
 * @Author: zzw
 */


// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String[] ym = in.nextLine().split("\\.");
            String[] ip1 = in.nextLine().split("\\.");
            String[] ip2 = in.nextLine().split("\\.");
            boolean flag = false;
            if (ip1.length == 0 || ym.length == 0 || ip2.length == 0) {
                continue;
            }
            for (int j = 0; j < 4; j++) {
                int ip1Int = Integer.parseInt(ip1[j]);
                int ymInt = Integer.parseInt(ym[j]);
                int ip2Int = Integer.parseInt(ip2[j]);
                //判断ip是否合规
                if ((0 > ip1Int || ip1Int > 255) || (0 > ymInt || ymInt > 255) ||
                        (0 > ip2Int || ip2Int > 255)) {
                    System.out.println(1);
                    flag = true;
                    break;
                }
                //ip转2进制高位补0
                ip1[j] = "00000000" + Integer.toBinaryString(ip1Int);
                ym[j] = "00000000" + Integer.toBinaryString(ymInt);
                ip2[j] = "00000000" + Integer.toBinaryString(ip2Int);
                ip1[j] = ip1[j].substring(ip1[j].length() - 8);
                ym[j] = ym[j].substring(ym[j].length() - 8);
                ip2[j] = ip2[j].substring(ip2[j].length() - 8);
            }
            if (!flag) {
                //掩码合法性判断
                if (!String.join("", ym).replaceAll("11+", "1").replaceAll("00+",
                        "0").equals("10")) {
                    System.out.println(1);
                    flag = true;
                }
                if (!flag) {
                    for (int j = 0; j < 32; j++) {
                        //判断子网是否相等
                        if (ym[j % 4].charAt(j / 4) == '1' &&
                                ip1[j % 4].charAt(j / 4) != ip2[j % 4].charAt(j / 4)) {
                            System.out.println(2);
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        System.out.println(0);
                    }
                }
            }
        }
    }
}